﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using iTextSharp.text.pdf;

namespace Grinder.Renamer.RenamePlugins
{
    internal sealed class PDFRenamePlugin : IRenamePlugin
    {
        private readonly Dictionary<string, string> _tokens;
        public string Name { get { return "PDF Files"; } }
        public IEnumerable<string> Extensions { get { return new List<string> { "*.PDF" }; } }
        public Dictionary<string, string> Tokens { get { return _tokens; } }
        public System.Drawing.Image Icon { get { return Properties.Resources.html; } }
        public override string ToString() { return string.Format("{0} ({1})", Name, string.Join(",", Extensions.ToArray()).ToLower()); }

        public PDFRenamePlugin()
        {
            _tokens = new Dictionary<string, string>
                         {
                             {"title",    "Document title"},
                             {"author",   "Document author"},
                             {"creator",  "Document creator"},
                             {"producer", "Document producer"},
                             {"date",     "Document creation date"},
                             {"mdate",    "Document modification date"}
                         };
        }

        public Dictionary<string, object> GetTokens(FileInfo file)
        {
            var dic = new Dictionary<string, object>();

            var reader = new PdfReader(file.FullName);

            dic.Add("title", reader.Info["Title"]);
            dic.Add("author", reader.Info["Author"]);
            dic.Add("creator", reader.Info["Creator"]);
            dic.Add("producer", reader.Info["Producer"]);
            dic.Add("date", reader.Info["CreationDate"]);
            dic.Add("mdate", reader.Info["ModDate"]);

            return dic;
        }
    }
}
